from django.shortcuts import render
from django.http.response import JsonResponse, HttpResponse
from django.core import serializers

import json
from .models import *

# Create your views here.


def get_carousel_imgs(request):
    if request.method == "GET":
        # db = Carousel.objects.all()
        json_str = json.dumps([i.imgUrl.url for i in Carousel.objects.all()])
        # for i in db:
        #     print(i.imgUrl.url)
        # db = serializers.serialize("json", [i.imgUrl.url for i in Carousel.objects.all()])
        return JsonResponse({"status": 0, "data": json_str, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})

def construct_democard_dict(i):
    return{
        "demoId": f"{i.category.id}",
        "title": i.category.name,
        "time": f"{i.create_date}",
        "detail": i.synopsis,
        "imgUrl": i.imgUrl.url,
    }

def get_democard_all(request):
    if request.method == "GET":
        json_str = json.dumps([construct_democard_dict(i) for i in DemoExample.objects.all()])
        return JsonResponse({"status": 0, "data": json_str, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})

def get_democard(request):
    if request.method == "GET":
        cardID = request.GET.get('cardID')
        json_str = json.dumps(construct_democard_dict(DemoExample.objects.get(category=cardID)))
            
        return JsonResponse({"status": 0, "data": json_str, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})



def get_demoimg_list(request):   
    if request.method == "GET":
        cardID = request.GET.get('cardID')
        # for i in DemoPicture.objects.filter(category=imgLID):
        #     print(i.imgUrl)
        json_str = json.dumps([i.imgUrl.url for i in DemoPicture.objects.filter(category=cardID)])
        return JsonResponse({"status": 0, "data": json_str, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})


def construct_product_dict(i):
    return {
        "pID": f"{i.id}",
        "title": i.name,
    }

def get_product_category(request):
    if request.method == "GET":
        json_str = json.dumps([construct_product_dict(i) for i in ProductCategory.objects.all()])
        return JsonResponse({"status": 0, "data": json_str, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})

def get_product_img(request):
    if request.method == "GET":
        pID = request.GET.get('pID')
        db = Product.objects.get(category=pID)
        
        return JsonResponse({"status": 0, "data": db.imgUrl.url, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})


def get_demo_top_img(request):
    if request.method == "GET":
        db = DemoTopPicture.objects.last()
        
        return JsonResponse({"status": 0, "data": db.imgUrl.url, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})

def get_about_us(request):
    if request.method == "GET":
        db = AboutUS.objects.last()
        json_data = {
            "synopsis":db.synopsis,
            "imgUrl":db.imgUrl.url,
        }
        json_txt = json.dumps(json_data)
        return JsonResponse({"status": 0, "data": json_txt, "message": "OK"})
    else:
        return JsonResponse({"status": 1, "data": None, "message": "Need Get Method"})



